/*! \file StringConversions.cpp
 *	\brief 
 *
 *	created:	2010/11/29
 *	created:	29:11:2010   16:16
 *	filename: 	C:\src\tactics-engine\libs\gd_dx11\StringConversions.cpp
 *	file path:	C:\src\tactics-engine\libs\gd_dx11
 *	file base:	StringConversions
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				vitalii.trubchaninov@ubisoft.com
 */

#include "gd_dx11/stdafx.h"
#include "gd_dx11/StringConversions.h"

namespace tt{
namespace gd_dx11{

namespace impl{

struct ToStringItem
{
	UInt value;
	const Char* name;
};

static const ToStringItem s_D3D_DriverTypes[] = {
	{ D3D_DRIVER_TYPE_HARDWARE, "D3D_DRIVER_TYPE_HARDWARE" },
	{ D3D_DRIVER_TYPE_WARP, "D3D_DRIVER_TYPE_WARP" },
	{ D3D_DRIVER_TYPE_REFERENCE, "D3D_DRIVER_TYPE_REFERENCE" }
};

static const ToStringItem s_D3D_FeatureLevel[] = {
	{ D3D_FEATURE_LEVEL_11_0, "D3D_FEATURE_LEVEL_11_0" },
	{ D3D_FEATURE_LEVEL_10_1, "D3D_FEATURE_LEVEL_10_1" },
	{ D3D_FEATURE_LEVEL_10_0, "D3D_FEATURE_LEVEL_10_0" },
	{ D3D_FEATURE_LEVEL_9_3, "D3D_FEATURE_LEVEL_9_3" },
	{ D3D_FEATURE_LEVEL_9_2, "D3D_FEATURE_LEVEL_9_2" },
};

static const ToStringItem s_D3D_DXGI_Format[] = {
	{ DXGI_FORMAT_R32G32B32_FLOAT, "DXGI_FORMAT_R32G32B32_FLOAT" },
	{ DXGI_FORMAT_R32G32B32A32_FLOAT, "DXGI_FORMAT_R32G32B32A32_FLOAT" },
};

//////////////////////////////////////////////////////////////////////////
const Char* ToString(const ToStringItem* items, size_t count, UInt value)
{
	for (size_t index = 0; index != count; ++index)
	{
		if (items[index].value == value)
			return items[index].name;
	}
	return NULL;
}

template<size_t Count>
const Char* ToString(const ToStringItem (&items)[Count], UInt value)
{
	return ToString(items, _countof(items), value);
}

//////////////////////////////////////////////////////////////////////////
UInt FromString(const ToStringItem* items, size_t count, const Char* value)
{
	for (size_t index = 0; index != count; ++index)
	{
		if ( strcmp(items[index].name, value) == 0 )
			return items[index].value;
	}
	return 0xFFFFFFFF;
}

template<typename Result, size_t Count>
Result FromString(const ToStringItem (&items)[Count], const Char* value)
{
	return static_cast<Result>( FromString(items, _countof(items), value) );
}

} //namespace impl

//////////////////////////////////////////////////////////////////////////
const Char* ToString( D3D_DRIVER_TYPE value )
{
	return impl::ToString(impl::s_D3D_DriverTypes, value);
}

const Char* ToString( D3D_FEATURE_LEVEL value )
{
	return impl::ToString(impl::s_D3D_FeatureLevel, value);
}

DXGI_FORMAT FromString( const Char* value )
{
	return impl::FromString<DXGI_FORMAT>(impl::s_D3D_DXGI_Format, value);
}

} //namespace gd_dx11
} //namespace tt